// SetBoundary.cpp : implementation file
//

#include "stdafx.h"
#include "yujinsrhost.h"
#include "SetBoundary.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetBoundary dialog


CSetBoundary::CSetBoundary(CWnd* pParent /*=NULL*/)
	: CDialog(CSetBoundary::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetBoundary)
	m_sTop = _T("");
	m_sLeft = _T("");
	m_sRight = _T("");
	m_sBottom = _T("");
	//}}AFX_DATA_INIT
}


void CSetBoundary::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetBoundary)
	DDX_Control(pDX, IDC_TOP_SCROLLBAR, m_Top_Scrollbar);
	DDX_Control(pDX, IDC_RIGHT_SCROLLBAR, m_Right_Scrollbar);
	DDX_Control(pDX, IDC_LEFT_SCROLLBAR, m_Left_Scrollbar);
	DDX_Control(pDX, IDC_BOTTOM_SCROLLBAR, m_Bottom_Scrollbar);
	DDX_Text(pDX, IDC_TOP_STATIC, m_sTop);
	DDX_Text(pDX, IDC_LEFT_STATIC, m_sLeft);
	DDX_Text(pDX, IDC_RIGHT_STATIC, m_sRight);
	DDX_Text(pDX, IDC_BOTTOM_STATIC, m_sBottom);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetBoundary, CDialog)
	//{{AFX_MSG_MAP(CSetBoundary)
	ON_WM_TIMER()
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetBoundary message handlers

void CSetBoundary::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
		MdigGrab(((CMainFrame*)AfxGetMainWnd())->MilDigitizer,((CMainFrame*)AfxGetMainWnd())->MilImageBuffer);

		if(MbufInquire(((CMainFrame*)AfxGetMainWnd())->MilImageBuffer, M_HOST_ADDRESS, &((CMainFrame*)AfxGetMainWnd())->pView->m_pImage) == NULL) //Memory Address Setup 
			AfxMessageBox("ERROR!!");

		MdigGrabWait(((CMainFrame*)AfxGetMainWnd())->MilDigitizer,M_GRAB_END);

		((CMainFrame*)AfxGetMainWnd())->pView->ConvertYUVRGB(((CMainFrame*)AfxGetMainWnd())->pView->m_pImage);

		HDC hdc = ::GetDC(((CMainFrame*)AfxGetMainWnd())->pView->m_hWnd);
		CDC NewDC;
		NewDC.Attach(hdc);
		BYTE *ptrBuf = ((CMainFrame*)AfxGetMainWnd())->pView->DisplayByteTypeImage; 
		long Data;
		for(int h = 0;h < HEIGHT;h++){
			for(int w = 0;w < WIDTH;w+=2){

				Data = (((CMainFrame*)AfxGetMainWnd())->pView->DisplayGroundArray[h][w-1]);
				*(ptrBuf+2) = (BYTE)((Data   >>  16)	& 0x000000ff);
				*(ptrBuf+1) = (BYTE)((Data >>  8 )		& 0x000000ff);
				*(ptrBuf  ) = (BYTE)((Data >>  0 )		& 0x000000ff);
				ptrBuf +=4;

				Data = (((CMainFrame*)AfxGetMainWnd())->pView->DisplayGroundArray[h][w]);
				*(ptrBuf+2) = (BYTE)((Data   >>  16)		& 0x000000ff);
				*(ptrBuf+1) = (BYTE)((Data >>  8 )		& 0x000000ff);
				*(ptrBuf  ) = (BYTE)((Data >>  0 )		& 0x000000ff);
				ptrBuf +=4;
			}
		}
		
		CBitmap		bitmap; 
		CDC			dcMemory;
	
	    dcMemory.CreateCompatibleDC(&NewDC);
	
		bitmap.CreateCompatibleBitmap(&NewDC,640,480);
		bitmap.SetBitmapBits(640*480*4,((CMainFrame*)AfxGetMainWnd())->pView->DisplayByteTypeImage);
		dcMemory.SelectObject(bitmap);
		
		CBrush objBrh,*oldobjBrh;
		objBrh.CreateHatchBrush(HS_DIAGCROSS, RGB(255,0,0));
		oldobjBrh=dcMemory.SelectObject(&objBrh);
		dcMemory.SetBkMode(TRANSPARENT);
		dcMemory.SetROP2(R2_MERGENOTPEN);
		dcMemory.Rectangle(rect.left, rect.top, rect.right, rect.bottom);
		dcMemory.SelectObject(oldobjBrh);
		
		NewDC.BitBlt(0,0,640,480,&dcMemory,0,0,SRCCOPY);

		bitmap.DeleteObject();
		objBrh.DeleteObject();
		NewDC.Detach();
		::ReleaseDC(((CMainFrame*)AfxGetMainWnd())->pView->m_hWnd,hdc );
		CDialog::OnTimer(nIDEvent);
}

void CSetBoundary::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	long nID, index;		
	int value;

	nID	= GetWindowLong(pScrollBar->m_hWnd, GWL_ID);		
	index = nID-IDC_TOP_SCROLLBAR;
	switch(index){
	case 0:                                
   		value = atoi((const char *)m_sTop);  	
		break;     
	case 1:                                
   		value = atoi((const char *)m_sLeft);	
		break;     
	case 2:                                
   		value = atoi((const char *)m_sBottom);  	  	
		break;     
	case 3:                                
   		value = atoi((const char *)m_sRight);  	  	
		break;     
	}	

	switch(nSBCode){                         
	case SB_LINEDOWN:
		value += 1; 
		break;
	case SB_LINEUP:   
		value += -1;
		break;
	case SB_PAGEDOWN:                               
		value += 5;
		break;						
	case SB_PAGEUP:                                                                                         
		value += -5;
		break;						                                  
	case SB_BOTTOM:
		value = 0;
		break;
	case SB_TOP:     
		value = 255;
		break;
	case SB_THUMBTRACK:
	case SB_THUMBPOSITION:
		value = nPos;
		break;																				
	default:						          
		CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
		return;
	}
	if(index == 0 || index == 2){
		if(value > HEIGHT-1)     
			value = HEIGHT-1;
		else if(value < 0)
			value = 0;
	}
	else{
		if(value > WIDTH-1)     
			value = WIDTH-1;
		else if(value < 0)
			value = 0;
	}
		
	pScrollBar->SetScrollPos(value); 
	SetDlgItemInt(nID, value, FALSE);		 	                   				

	char tmp[25];
	sprintf(tmp, "%d", value); 
	switch(index){
	case 0:                                
    	m_sTop = tmp; 
		rect.top = value;
    	break;
	case 1:                
		m_sLeft = tmp; 
		rect.left = value;
    	break;
	case 2:                
		m_sBottom = tmp; 
		rect.bottom = value;
    	break;
	case 3:                
		m_sRight = tmp; 
		rect.right = value;
    	break;
	}                 
	UpdateData(FALSE);
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

BOOL CSetBoundary::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	char sPos[25];
	
	m_Top_Scrollbar.SetScrollRange(0,HEIGHT-1);
	m_Left_Scrollbar.SetScrollRange(0,WIDTH-1);
	m_Bottom_Scrollbar.SetScrollRange(0,HEIGHT-1);
	m_Right_Scrollbar.SetScrollRange(0,WIDTH-1);
	
	m_Top_Scrollbar.SetScrollPos(rect.top);
	m_Left_Scrollbar.SetScrollPos(rect.left);
	m_Bottom_Scrollbar.SetScrollPos(rect.bottom);
	m_Right_Scrollbar.SetScrollPos(rect.right);
		
	itoa(m_Top_Scrollbar.GetScrollPos(),sPos,10);
	m_sTop = sPos;
	itoa(m_Left_Scrollbar.GetScrollPos(),sPos,10);
	m_sLeft = sPos;
	itoa(m_Bottom_Scrollbar.GetScrollPos(),sPos,10);
	m_sBottom = sPos;
	itoa(m_Right_Scrollbar.GetScrollPos(),sPos,10);
	m_sRight = sPos;
	UpdateData(FALSE);

	SetTimer(1, 1000, NULL);	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
